package com.qf.controller.business;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entitys.*;
import com.qf.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/business/order")
public class OrderController {
    @Resource
    private OrderService orderService;
    @Resource
    private MenuItemService menuItemService;
    @Resource
    private CouponService couponService;
    @Resource
    private MenuItemSpecificationsService specService;
    @Resource
    private ComorderService comorderService;
    @Resource
    private UserCouponService userCouponService;
    @Resource
    private ShipAddressService shipAddressService;

    /**
     * 全部订单
     * @param pageindex
     * @param request
     * @return
     */
    @GetMapping("/list")
    public String list(Integer pageindex, HttpServletRequest request){
        HttpSession session = request.getSession();
        Business business=(Business) session.getAttribute("login");
        PageInfo<Order> pageInfo = orderService.selectPayBusiness(business.getBid(),pageindex);
        Integer list=0;
        request.setAttribute("pageinfo",pageInfo);
        session.setAttribute("list",list);
        return "business/orderlist";
    }

    /**
     * 待处理订单
     * @param pageindex
     * @param request
     * @return
     */
    @GetMapping("/waitList")
    public String waitList(Integer pageindex, HttpServletRequest request){
        HttpSession session = request.getSession();
        Business business=(Business) session.getAttribute("login");
        PageInfo<Order> pageInfo = orderService.queryPayBusiness(business.getBid(),pageindex,2);
        Integer list=2;
        request.setAttribute("pageinfo",pageInfo);
        session.setAttribute("list",list);
        return "business/orderlist";
    }
    /**
     * 已完成订单
     * @param pageindex
     * @param request
     * @return
     */
    @GetMapping("/overList")
    public String overList(Integer pageindex, HttpServletRequest request){
        HttpSession session = request.getSession();
        Business business=(Business) session.getAttribute("login");
        PageInfo<Order> pageInfo = orderService.queryPayBusiness(business.getBid(),pageindex,5);
        Integer list=5;
        request.setAttribute("pageinfo",pageInfo);
        request.setAttribute("list",list);
        return "business/orderlist";
    }
    /**
     * 已取消订单
     * @param pageindex
     * @param request
     * @return
     */
    @GetMapping("/cancelList")
    public String cancelList(Integer pageindex, HttpServletRequest request){
        HttpSession session = request.getSession();
        Business business=(Business) session.getAttribute("login");
        PageInfo<Order> pageInfo = orderService.queryPayBusiness(business.getBid(),pageindex,6);
        Integer list=6;
        request.setAttribute("pageinfo",pageInfo);
        session.setAttribute("list",list);
        return "business/orderlist";
    }

    @GetMapping("/detail")
    public String detail(String outTradeNo,HttpServletRequest request){
        Order order = orderService.selectByPrimaryKeyAndShipaddress(outTradeNo);
        List<Comorder> comorders = comorderService.queryByOutTradeNo(outTradeNo);
        List<MenuItemSpecifications> menuItemSpecifications = specService.selectByouttradeNo(outTradeNo);
        request.setAttribute("order",order);
        request.setAttribute("co",comorders);
        request.setAttribute("ms",menuItemSpecifications);
        return "business/orderdetail";
    }
    @GetMapping("/deliver")
    @ResponseBody
    public RespBean deliver(String no){
        Order order = orderService.selectByPrimaryKey(no);
        order.setOrderType(3);
        int i = orderService.updateByPrimaryKeySelective(order);
        if (i>0){
            return RespBean.success("发货成功，即将返回列表!");
        }else {
            return RespBean.error("发货失败!");
        }
    }
    @GetMapping("/cancel")
    @ResponseBody
    public RespBean cancel(String no){
        Order order = orderService.selectByPrimaryKey(no);
        order.setOrderType(6);
        int i = orderService.updateByPrimaryKeySelective(order);
        if (i>0){
            return RespBean.success("取消订单成功，即将返回列表!");
        }else {
            return RespBean.error("取消订单失败!");
        }
    }

    @GetMapping("/del")
    @ResponseBody
    public RespBean del(String no){
        Order order = orderService.selectByPrimaryKey(no);
        order.setOrderType(7);
        int i = orderService.updateByPrimaryKeySelective(order);
        if (i>0){
            return RespBean.success("删除成功");
        }else {
            return RespBean.error("删除失败!");
        }
    }

    @GetMapping("/query")
    public String query(String no,HttpServletRequest request){
        HttpSession session = request.getSession();
        PageHelper.startPage(1,10);
        Business business=(Business) session.getAttribute("login");
        Object list = session.getAttribute("list");
        List<Order> orders;
        if ((Integer) list==0){
            orders= orderService.queryByPrimaryKey(business.getBid(), no);
        }else {
            orders= orderService.queryByPrimaryKeyAndType(business.getBid(), no,(Integer) list);
        }
        PageInfo<Order> pageInfo=new PageInfo<>(orders);
        System.out.println(pageInfo.getList());
        request.setAttribute("pageinfo",pageInfo);
        return "business/orderlist";
    }
}
